package us.clubadvocate.client.index;

import java.util.ArrayList;
import java.util.List;

import us.clubadvocate.shared.Pet;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

class IndexView extends Composite
{
  final static ClubRpcServiceAsync srvc = 
      (ClubRpcServiceAsync)GWT.create(ClubRpcService.class);
  
  static IndexPageUiBinder uiBinder = GWT.create(IndexPageUiBinder.class);
  
  @UiTemplate("index.ui.xml")
  interface IndexPageUiBinder extends UiBinder<HTMLPanel, IndexView>{}
  
  @UiField
  TextBox tboxPetName;
  
  @UiField
  Button btnSubmit, btnSave;
  
  @UiField
  TextArea txtOutput;
  
  String recordTxt;
  List<String> fileData = new ArrayList<String>();
  
  // ---------------------------------------------------------------------------
  // ---------------------------------------------------------------------------
  
  IndexView()
  {
    initWidget(uiBinder.createAndBindUi(this));
  }
  
  // ---------------------------------------------------------------------------
  
  @UiHandler("btnSubmit")
  public void doSubmit(ClickEvent e)
  {
    recordTxt = tboxPetName.getText();
    System.out.print("Text: " + recordTxt);
    tboxPetName.setText("");
    txtOutput.setText("Looking for: " + recordTxt + "\n");
    srvc.getRecord(recordTxt, new GetRecordCB());
  }
  
  // ---------------------------------------------------------------------------
  
  @UiHandler("btnSave")
  public void doSave(ClickEvent e)
  {
    fileData.add("Test Text");
    FileSaver.saveStringToFile(fileData, "data.txt");
    
    /*if(FileSaver.checkSaveAs())
      txtOutput.setText("True");
    else
      txtOutput.setText("false");*/
  }
  
  // ---------------------------------------------------------------------------
  // ---------------------------------------------------------------------------
  
  class GetRecordCB implements AsyncCallback<Pet>
  {
    @Override
    public void onFailure(Throwable caught)
    {
      caught.printStackTrace();
      txtOutput.setText("Failed to get Record" +  caught.toString() + "\n");
    }

    @Override
    public void onSuccess(Pet result)
    {
      StringBuilder b = new StringBuilder();
      b.append("Looking for: ").append(recordTxt).append("\n");
      if(result == null)
        b.append("!!!!  Null result  !!!!\n");
      else
        b.append(result.toString());
      txtOutput.setText(b.toString());
    }
  }
  // ---------------------------------------------------------------------------
}
